package messengerclient;

import messengerserver.DBExch.PText;
import java.rmi.RemoteException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import messengerclient.observers.ObsGUI;
import messengerclient.observers.ObsNew;
import messengerclient.observers.ObsTable;
import messengercommon.dto.Message;
import messengercommon.person.Contact;
import messengercommon.person.Person;

/**
 *
 * @author Samus
 */
public class Controller implements ContObservable {

    private Person person = null;
    private static Controller instance = new Controller();
    private HashMap<String, ObsGUI> observers = new HashMap<String, ObsGUI>();
    private ObsTable list = null;
    private ObsNew chatController = null;

    public static Controller getInstance() {
        return instance;
    }

    public void assignPerson(Person person) {
        this.person = person;
    }

    public void assignTable(ObsTable obs) {
        this.list = obs;
    }

    public void assignChatController(ObsNew CC) {
        this.chatController = CC;
    }

    public void addObsList(String nombre, ObsGUI obs) {
        observers.put(nombre, obs);
    }

    public void sendMessage(Message message) throws RemoteException {
        Client.getInstance().sendMessage(message);
        Message message2 = new Message();
        message2.setOrigen(message.getOrigen());
        message2.setMensaje(message.getMensaje());
        observers.get(message.getDestino()).receiveMessage(message2);
    }

    @Override
    public int autentifyUser(String user, String password) throws Exception {
        Person person2 = new Person();
        person2.setUsername(user);
        person2.setPassword(password);
        int res = 0;
        try {
            res = Client.getInstance().autentifyUser(person2);
            if (res == 1) {
                this.person = Client.getInstance().userData(person2);
            }
        } catch (Exception ex) {
            Client.getInstance().userDisconnect(person2);
            throw ex;
        }
        return res;
    }

    public void contactList(List<Contact> contactlist) {
        this.person.setContacts(contactlist);
        if (person.getContacts().size() > 0) {
            showContacts();
        }
    }

    @Override
    public void nickChange(String nick) {
        this.person.setNick(nick);
        try {
            Client.getInstance().nickChange(this.person);
        } catch (RemoteException ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void userDisconnect() {
        try {
            Client.getInstance().userDisconnect(this.person);
        } catch (RemoteException ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void receiveMessage(Message message) {
        if (!observers.containsKey(message.getOrigen())) {
            chatController.newWindow(message);
        } else {
            observers.get(message.getOrigen()).receiveMessage(message);
        }
    }

    public void removeObs(String nom) {
        observers.remove(nom);
    }

    public void contactChange() throws RemoteException {
        readConList();
    }

    public boolean newUser(String user, String password, String nick) throws RemoteException {
        Person p = new Person();
        p.setNick(nick);
        p.setUsername(user);
        p.setPassword(password);
        return Client.getInstance().newUser(p);
    }

    public int addContact(String nomb) throws RemoteException {
        return Client.getInstance().addContact(person, nomb);
    }

    public void readConList() throws RemoteException {
        Client.getInstance().readConList(this.person);
    }

    public void showContacts() {
        List<Contact> list2 = person.getContacts();
        list.receiveContacts(list2);
    }

    public String getPersonName() {
        return person.getUsername();
    }

    public Contact darContacto(String username) {
        Contact con = null;
        for (Contact c : person.getContacts()) {
            if (c.getUsername().equals(username)) {
                con = c;
            }
        }
        return con;
    }

    public String getNick() {
        return this.person.getNick();
    }

    public void setNewNick(String nick) throws RemoteException {
        this.person.setNick(nick);
        Client.getInstance().nickChange(this.person);
    }

    public ArrayList<String> readLog() throws RemoteException {
        return Client.getInstance().readLog();
    }
}
